package com.ideaaedi.zoo.diy.feature.reqresp.encdec.impl.springmvc.core;

import com.ideaaedi.commonds.security.AES;
import com.ideaaedi.commonds.security.Base64;
import com.ideaaedi.zoo.diy.feature.reqresp.encdec.api.executor.AesExecutor;
import com.ideaaedi.zoo.diy.feature.reqresp.encdec.api.properties.ZooReqrespEncdecProperties;

import javax.annotation.Nonnull;
import java.nio.charset.StandardCharsets;

/**
 * 请求响应加解密AES工具
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
public class DefaultAesExecutor implements AesExecutor {
    
    private final ZooReqrespEncdecProperties zooReqrespEncdecProperties;
    
    public DefaultAesExecutor(ZooReqrespEncdecProperties zooReqrespEncdecProperties) {
        this.zooReqrespEncdecProperties = zooReqrespEncdecProperties;
    }
    
    @Nonnull
    @Override
    public String encrypt(@Nonnull String plain) {
        return AES.encryptSilently(
                plain,
                Base64.encode(
                        zooReqrespEncdecProperties.getRespEncrypt().getAesKey().getBytes(StandardCharsets.UTF_8)
                )
        );
    }
    
    @Nonnull
    @Override
    public String decrypt(@Nonnull String cipher) {
        return AES.decryptSilently(
                cipher,
                Base64.encode(
                        zooReqrespEncdecProperties.getReqDecrypt().getAesKey().getBytes(StandardCharsets.UTF_8)
                )
        );
    }
}
